/** 创建组件对象 * */
var indicatorDataForm = new Form();
var indicatorDataTable = new Table();
var indicatorDataPage = new Page();
var indicatorDataAjax = new Ajax();
var flag = false;

/** 页面加载完执行 * */
$(function() {
	flag = sessionStorage.orgCode == "*";
	initScript();
	initConditions();
	initDate();
	pageQuery();
	// initPage();
	// initTable();
	resetTableHeight();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/data/locale/IndicatorData_" + appLocale + ".js"); // 1.加载国际化js*
	loadScript(appPath + "/project-yddemo/data/properties/IndicatorDataMonthResultProperties.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}

/** 初始化查询条件 * */
function initConditions() {
	indicatorDataForm.initTableConditions({
		divId : "dataMonthResultQueryDiv", // 1.归属div*
		type : "table",
		conditions : indicatorDataProperties.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			action : "yddemo:dataMonthResult:view",
			width : "120", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : indicatorDataLocale.text.search, // 文本*
			clickFun : function() { // 按钮事件*
				// indicatorDataPage.setCurrPage(1);
				pageQuery();
			}
		}
	});

}

/** 初始化分页 * */
function initPage() {
	indicatorDataPage.initPage({
		divId : "dataMonthResultPageDiv", // 1.归属div*
		eachPageNum : 10, // 2.每页条数(5,10,15,20)*
		pageChangeFun : function() { // 3.分页刷新事件*
			pageQuery();
		}
	});
}

/** 分页查询 * */
function initTable() {
	indicatorDataTable.initTable({
		divId : "dataMonthResultTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/yddemo/dataMonthResults", // 请求url*
			type : "GET", // 请求类型*
			page : indicatorDataPage
		// 分页对象(需要分页时配置)
		},
		columns : indicatorDataProperties.getViewColumns(),// 3.表格列配置
		rowClickFun : function(datas) {
		}
	});
	pageQuery();
}

/** 分页查询 * */
function pageQuery() {
	// 查询时候如果存在就删除
	var table = $("#monthResultTable");
	if (table) {
		$("#monthResultTable").remove();
	}

	var conditions = indicatorDataForm.getQueryConditions();

	var dataObj = indicatorDataAjax.sendAjax({ // 获取用户对象
		url : requestPath + "/yddemo/dataMonthResults", // 请求url*
		async : false, // 是否异步*
		type : "GET", // 请求类型*
		data : conditions
	});
	var indicatorList = dataObj.indicatorList;
	var companyList = dataObj.companyList;
	var indicatorDataList = dataObj.result;

	if (indicatorList) {
		var widthValue = indicatorList.length*10+10;
		// 指标存在才画表格
		// 画表头
		var rowNum = 0;
		var table = document.createElement("table");
		table.id = "monthResultTable";
//		table.style.width = "230%";
		table.style.width = widthValue+"%";

		var row = table.insertRow(rowNum++);
		var cell = row.insertCell(0);
		cell.innerHTML = indicatorDataLocale.field.companys;
		cell.width = "10%";
		cell.style = "text-align:center";

		for (var i = 0; i < indicatorList.length; i++) {
			cell = row.insertCell(i + 1);
			cell.innerHTML = indicatorList[i].name;
			cell.width="10%";
			cell.style = "text-align:center";
		}

		// 画数据行
		var row;
		var cellNo;
		for (var j = 0; j < indicatorDataList.length; j++) {
			var indicatorData = indicatorDataList[j];
			var indicator = indicatorData.indicatorDetail.indicator;
			var company = indicatorData.indicatorDetail.company;
			var dataVal = indicatorData.value;

			if (j % indicatorList.length == 0) {
				// 创建行，并且画第1、2个单元格
				row = table.insertRow(rowNum++);
				var cell = row.insertCell(0);
				cell.width = "10%";
				cell.style = "text-align:center";
				cell.innerHTML = company.name;

				var cell = row.insertCell(1);
				cell.width = "10%";
				cell.style = "text-align:center";
				if(dataVal && dataVal != null && dataVal != "null"){
					cell.innerHTML = dataVal;
				}else{
					cell.innerHTML = "";
				}

				cellNo = 2;
			}else{
				var cell = row.insertCell(cellNo++);
				cell.width = "10%";
				cell.style = "text-align:center";
				if(dataVal && dataVal != null && dataVal != "null"){
					cell.innerHTML = dataVal;
				}else{
					cell.innerHTML = "";
				}
			}

		}

		$("#dataMonthResultTableDiv").append(table);
	}

}

function initDate() {
	$("#dataMonthResultQueryDiv").find("[key = 'date']").jeDate({
		isinitVal : true,
		initAddVal : {
			YYYY : 0,
			MM : -0,
			DD : -0
		},
		// //初始化年月日(加减)，common.js提供getJeDateAddVal("2008-08-08")
		// hmsSetVal:{YYYY:2008,MM:8,DD:8,hh:8,mm:8,ss:8}, //1.初始化年月日时分秒
		// 2.初始化时分秒
		ishmsVal : false,
		// minDate: '2016-06-16 23:59:59',
		// maxDate: $.nowDate({DD:0}),
		format : "YYYY-MM",
		zIndex : 3000,
		language : getJeDateLanguage()
	});
}

/**
 * 重设表格高度
 * 
 * @param
 * @return
 */
function resetTableHeight() {
	var leftHeight = $(".dataListDiv").height() - getTotalHeight([ "dataMonthResultQueryDiv" ]);// ,
	// "dataMonthResultButtonDiv","dataMonthResultPageDiv"
	$("#dataMonthResultTableDiv").height(leftHeight);
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight();
});
